<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>disposable.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_disposable_disposable.js.html">disposable.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2005 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Implements the disposable interface. The dispose method is used
<a name="line17"></a> * to clean up references and resources.
<a name="line18"></a> * @author arv@google.com (Erik Arvidsson)
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>
<a name="line22"></a>goog.provide(&#39;goog.Disposable&#39;);
<a name="line23"></a>goog.provide(&#39;goog.dispose&#39;);
<a name="line24"></a>
<a name="line25"></a>goog.require(&#39;goog.disposable.IDisposable&#39;);
<a name="line26"></a>
<a name="line27"></a>
<a name="line28"></a>
<a name="line29"></a>/**
<a name="line30"></a> * Class that provides the basic implementation for disposable objects. If your
<a name="line31"></a> * class holds one or more references to COM objects, DOM nodes, or other
<a name="line32"></a> * disposable objects, it should extend this class or implement the disposable
<a name="line33"></a> * interface (defined in goog.disposable.IDisposable).
<a name="line34"></a> * @constructor
<a name="line35"></a> * @implements {goog.disposable.IDisposable}
<a name="line36"></a> */
<a name="line37"></a>goog.Disposable = function() {
<a name="line38"></a>  if (goog.Disposable.MONITORING_MODE != goog.Disposable.MonitoringMode.OFF) {
<a name="line39"></a>    this.creationStack = new Error().stack;
<a name="line40"></a>    goog.Disposable.instances_[goog.getUid(this)] = this;
<a name="line41"></a>  }
<a name="line42"></a>};
<a name="line43"></a>
<a name="line44"></a>
<a name="line45"></a>/**
<a name="line46"></a> * @enum {number} Different monitoring modes for Disposable.
<a name="line47"></a> */
<a name="line48"></a>goog.Disposable.MonitoringMode = {
<a name="line49"></a>  /**
<a name="line50"></a>   * No monitoring.
<a name="line51"></a>   */
<a name="line52"></a>  OFF: 0,
<a name="line53"></a>  /**
<a name="line54"></a>   * Creating and disposing the goog.Disposable instances is monitored. All
<a name="line55"></a>   * disposable objects need to call the {@code goog.Disposable} base
<a name="line56"></a>   * constructor. The PERMANENT mode must bet switched on before creating any
<a name="line57"></a>   * goog.Disposable instances.
<a name="line58"></a>   */
<a name="line59"></a>  PERMANENT: 1,
<a name="line60"></a>  /**
<a name="line61"></a>   * INTERACTIVE mode can be switched on and off on the fly without producing
<a name="line62"></a>   * errors. It also doesn&#39;t warn if the disposable objects don&#39;t call the
<a name="line63"></a>   * {@code goog.Disposable} base constructor.
<a name="line64"></a>   */
<a name="line65"></a>  INTERACTIVE: 2
<a name="line66"></a>};
<a name="line67"></a>
<a name="line68"></a>
<a name="line69"></a>/**
<a name="line70"></a> * @define {number} The monitoring mode of the goog.Disposable
<a name="line71"></a> *     instances. Default is OFF. Switching on the monitoring is only
<a name="line72"></a> *     recommended for debugging because it has a significant impact on
<a name="line73"></a> *     performance and memory usage. If switched off, the monitoring code
<a name="line74"></a> *     compiles down to 0 bytes.
<a name="line75"></a> */
<a name="line76"></a>goog.Disposable.MONITORING_MODE = 0;
<a name="line77"></a>
<a name="line78"></a>
<a name="line79"></a>/**
<a name="line80"></a> * Maps the unique ID of every undisposed {@code goog.Disposable} object to
<a name="line81"></a> * the object itself.
<a name="line82"></a> * @type {!Object.&lt;number, !goog.Disposable&gt;}
<a name="line83"></a> * @private
<a name="line84"></a> */
<a name="line85"></a>goog.Disposable.instances_ = {};
<a name="line86"></a>
<a name="line87"></a>
<a name="line88"></a>/**
<a name="line89"></a> * @return {!Array.&lt;!goog.Disposable&gt;} All {@code goog.Disposable} objects that
<a name="line90"></a> *     haven&#39;t been disposed of.
<a name="line91"></a> */
<a name="line92"></a>goog.Disposable.getUndisposedObjects = function() {
<a name="line93"></a>  var ret = [];
<a name="line94"></a>  for (var id in goog.Disposable.instances_) {
<a name="line95"></a>    if (goog.Disposable.instances_.hasOwnProperty(id)) {
<a name="line96"></a>      ret.push(goog.Disposable.instances_[Number(id)]);
<a name="line97"></a>    }
<a name="line98"></a>  }
<a name="line99"></a>  return ret;
<a name="line100"></a>};
<a name="line101"></a>
<a name="line102"></a>
<a name="line103"></a>/**
<a name="line104"></a> * Clears the registry of undisposed objects but doesn&#39;t dispose of them.
<a name="line105"></a> */
<a name="line106"></a>goog.Disposable.clearUndisposedObjects = function() {
<a name="line107"></a>  goog.Disposable.instances_ = {};
<a name="line108"></a>};
<a name="line109"></a>
<a name="line110"></a>
<a name="line111"></a>/**
<a name="line112"></a> * Whether the object has been disposed of.
<a name="line113"></a> * @type {boolean}
<a name="line114"></a> * @private
<a name="line115"></a> */
<a name="line116"></a>goog.Disposable.prototype.disposed_ = false;
<a name="line117"></a>
<a name="line118"></a>
<a name="line119"></a>/**
<a name="line120"></a> * Callbacks to invoke when this object is disposed.
<a name="line121"></a> * @type {Array.&lt;!Function&gt;}
<a name="line122"></a> * @private
<a name="line123"></a> */
<a name="line124"></a>goog.Disposable.prototype.onDisposeCallbacks_;
<a name="line125"></a>
<a name="line126"></a>
<a name="line127"></a>/**
<a name="line128"></a> * If monitoring the goog.Disposable instances is enabled, stores the creation
<a name="line129"></a> * stack trace of the Disposable instance.
<a name="line130"></a> * @type {string}
<a name="line131"></a> */
<a name="line132"></a>goog.Disposable.prototype.creationStack;
<a name="line133"></a>
<a name="line134"></a>
<a name="line135"></a>/**
<a name="line136"></a> * @return {boolean} Whether the object has been disposed of.
<a name="line137"></a> * @override
<a name="line138"></a> */
<a name="line139"></a>goog.Disposable.prototype.isDisposed = function() {
<a name="line140"></a>  return this.disposed_;
<a name="line141"></a>};
<a name="line142"></a>
<a name="line143"></a>
<a name="line144"></a>/**
<a name="line145"></a> * @return {boolean} Whether the object has been disposed of.
<a name="line146"></a> * @deprecated Use {@link #isDisposed} instead.
<a name="line147"></a> */
<a name="line148"></a>goog.Disposable.prototype.getDisposed = goog.Disposable.prototype.isDisposed;
<a name="line149"></a>
<a name="line150"></a>
<a name="line151"></a>/**
<a name="line152"></a> * Disposes of the object. If the object hasn&#39;t already been disposed of, calls
<a name="line153"></a> * {@link #disposeInternal}. Classes that extend {@code goog.Disposable} should
<a name="line154"></a> * override {@link #disposeInternal} in order to delete references to COM
<a name="line155"></a> * objects, DOM nodes, and other disposable objects. Reentrant.
<a name="line156"></a> *
<a name="line157"></a> * @return {void} Nothing.
<a name="line158"></a> * @override
<a name="line159"></a> */
<a name="line160"></a>goog.Disposable.prototype.dispose = function() {
<a name="line161"></a>  if (!this.disposed_) {
<a name="line162"></a>    // Set disposed_ to true first, in case during the chain of disposal this
<a name="line163"></a>    // gets disposed recursively.
<a name="line164"></a>    this.disposed_ = true;
<a name="line165"></a>    this.disposeInternal();
<a name="line166"></a>    if (goog.Disposable.MONITORING_MODE != goog.Disposable.MonitoringMode.OFF) {
<a name="line167"></a>      var uid = goog.getUid(this);
<a name="line168"></a>      if (goog.Disposable.MONITORING_MODE ==
<a name="line169"></a>          goog.Disposable.MonitoringMode.PERMANENT &amp;&amp;
<a name="line170"></a>          !goog.Disposable.instances_.hasOwnProperty(uid)) {
<a name="line171"></a>        throw Error(this + &#39; did not call the goog.Disposable base &#39; +
<a name="line172"></a>            &#39;constructor or was disposed of after a clearUndisposedObjects &#39; +
<a name="line173"></a>            &#39;call&#39;);
<a name="line174"></a>      }
<a name="line175"></a>      delete goog.Disposable.instances_[uid];
<a name="line176"></a>    }
<a name="line177"></a>  }
<a name="line178"></a>};
<a name="line179"></a>
<a name="line180"></a>
<a name="line181"></a>/**
<a name="line182"></a> * Associates a disposable object with this object so that they will be disposed
<a name="line183"></a> * together.
<a name="line184"></a> * @param {goog.disposable.IDisposable} disposable that will be disposed when
<a name="line185"></a> *     this object is disposed.
<a name="line186"></a> */
<a name="line187"></a>goog.Disposable.prototype.registerDisposable = function(disposable) {
<a name="line188"></a>  this.addOnDisposeCallback(goog.partial(goog.dispose, disposable));
<a name="line189"></a>};
<a name="line190"></a>
<a name="line191"></a>
<a name="line192"></a>/**
<a name="line193"></a> * Invokes a callback function when this object is disposed. Callbacks are
<a name="line194"></a> * invoked in the order in which they were added.
<a name="line195"></a> * @param {function(this:T):?} callback The callback function.
<a name="line196"></a> * @param {T=} opt_scope An optional scope to call the callback in.
<a name="line197"></a> * @template T
<a name="line198"></a> */
<a name="line199"></a>goog.Disposable.prototype.addOnDisposeCallback = function(callback, opt_scope) {
<a name="line200"></a>  if (!this.onDisposeCallbacks_) {
<a name="line201"></a>    this.onDisposeCallbacks_ = [];
<a name="line202"></a>  }
<a name="line203"></a>  this.onDisposeCallbacks_.push(goog.bind(callback, opt_scope));
<a name="line204"></a>};
<a name="line205"></a>
<a name="line206"></a>
<a name="line207"></a>/**
<a name="line208"></a> * Deletes or nulls out any references to COM objects, DOM nodes, or other
<a name="line209"></a> * disposable objects. Classes that extend {@code goog.Disposable} should
<a name="line210"></a> * override this method.
<a name="line211"></a> * Not reentrant. To avoid calling it twice, it must only be called from the
<a name="line212"></a> * subclass&#39; {@code disposeInternal} method. Everywhere else the public
<a name="line213"></a> * {@code dispose} method must be used.
<a name="line214"></a> * For example:
<a name="line215"></a> * &lt;pre&gt;
<a name="line216"></a> *   mypackage.MyClass = function() {
<a name="line217"></a> *     goog.base(this);
<a name="line218"></a> *     // Constructor logic specific to MyClass.
<a name="line219"></a> *     ...
<a name="line220"></a> *   };
<a name="line221"></a> *   goog.inherits(mypackage.MyClass, goog.Disposable);
<a name="line222"></a> *
<a name="line223"></a> *   mypackage.MyClass.prototype.disposeInternal = function() {
<a name="line224"></a> *     // Dispose logic specific to MyClass.
<a name="line225"></a> *     ...
<a name="line226"></a> *     // Call superclass&#39;s disposeInternal at the end of the subclass&#39;s, like
<a name="line227"></a> *     // in C++, to avoid hard-to-catch issues.
<a name="line228"></a> *     goog.base(this, &#39;disposeInternal&#39;);
<a name="line229"></a> *   };
<a name="line230"></a> * &lt;/pre&gt;
<a name="line231"></a> * @protected
<a name="line232"></a> */
<a name="line233"></a>goog.Disposable.prototype.disposeInternal = function() {
<a name="line234"></a>  if (this.onDisposeCallbacks_) {
<a name="line235"></a>    while (this.onDisposeCallbacks_.length) {
<a name="line236"></a>      this.onDisposeCallbacks_.shift()();
<a name="line237"></a>    }
<a name="line238"></a>  }
<a name="line239"></a>};
<a name="line240"></a>
<a name="line241"></a>
<a name="line242"></a>/**
<a name="line243"></a> * Returns True if we can verify the object is disposed.
<a name="line244"></a> * Calls {@code isDisposed} on the argument if it supports it.  If obj
<a name="line245"></a> * is not an object with an isDisposed() method, return false.
<a name="line246"></a> * @param {*} obj The object to investigate.
<a name="line247"></a> * @return {boolean} True if we can verify the object is disposed.
<a name="line248"></a> */
<a name="line249"></a>goog.Disposable.isDisposed = function(obj) {
<a name="line250"></a>  if (obj &amp;&amp; typeof obj.isDisposed == &#39;function&#39;) {
<a name="line251"></a>    return obj.isDisposed();
<a name="line252"></a>  }
<a name="line253"></a>  return false;
<a name="line254"></a>};
<a name="line255"></a>
<a name="line256"></a>
<a name="line257"></a>/**
<a name="line258"></a> * Calls {@code dispose} on the argument if it supports it. If obj is not an
<a name="line259"></a> *     object with a dispose() method, this is a no-op.
<a name="line260"></a> * @param {*} obj The object to dispose of.
<a name="line261"></a> */
<a name="line262"></a>goog.dispose = function(obj) {
<a name="line263"></a>  if (obj &amp;&amp; typeof obj.dispose == &#39;function&#39;) {
<a name="line264"></a>    obj.dispose();
<a name="line265"></a>  }
<a name="line266"></a>};
<a name="line267"></a>
<a name="line268"></a>
<a name="line269"></a>/**
<a name="line270"></a> * Calls {@code dispose} on each member of the list that supports it. (If the
<a name="line271"></a> * member is an ArrayLike, then {@code goog.disposeAll()} will be called
<a name="line272"></a> * recursively on each of its members.) If the member is not an object with a
<a name="line273"></a> * {@code dispose()} method, then it is ignored.
<a name="line274"></a> * @param {...*} var_args The list.
<a name="line275"></a> */
<a name="line276"></a>goog.disposeAll = function(var_args) {
<a name="line277"></a>  for (var i = 0, len = arguments.length; i &lt; len; ++i) {
<a name="line278"></a>    var disposable = arguments[i];
<a name="line279"></a>    if (goog.isArrayLike(disposable)) {
<a name="line280"></a>      goog.disposeAll.apply(null, disposable);
<a name="line281"></a>    } else {
<a name="line282"></a>      goog.dispose(disposable);
<a name="line283"></a>    }
<a name="line284"></a>  }
<a name="line285"></a>};
</pre>


</body>
</html>
